import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:jetour_line/base_widget/base_scaffold.dart';
import 'package:jetour_line/base_widget/loading_widget.dart';
import 'package:jetour_line/base_widget/my_refresh_view.dart';
import 'package:jetour_line/models/order_list_model.dart';
import 'package:jetour_line/pages/baseWidget/title_widget.dart';
import 'package:jetour_line/pages/line/lineDetail/line_detail_page.dart';
import 'package:jetour_line/pages/mine/order_detail_page.dart';
import 'package:jetour_line/utils/extension_int.dart';
import 'package:jetour_line/utils/page_push_utils.dart';

import '../../base_widget/base_image.dart';
import '../../base_widget/my_alert.dart';
import '../../my_colors.dart';
import '../../network/aha_http.dart';

class MineOrderPageScreen extends StatefulWidget {
  const MineOrderPageScreen({Key? key}) : super(key: key);

  @override
  State<MineOrderPageScreen> createState() => _MineOrderPageScreenState();
}

class _MineOrderPageScreenState extends State<MineOrderPageScreen> {
  final MyRefreshViewController _controller = MyRefreshViewController();
  int page = 1;
  List<ListElement> orderList = [];
  int loadingType = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getOrderList();
  }

  @override
  Widget build(BuildContext context) {
    return BaseScaffold(
      backgroundColor: MyColors.colorF1F1F1,
      title: "我的订单",
      body: orderList.isEmpty
          ? LoadingWidget(
        type: loadingType,
      )
          : MyRefreshView(
        refreshCallBack: (bool isUp) {
          if(isUp){
            page = 1;
          }else{
            page ++;
          }
          getOrderList();
        },
        controller: _controller,
        child: ListView.builder(
            itemCount: orderList.length,
            itemBuilder: (context, index) {
              return GestureDetector(onTap: (){
                PagePushUtils.push(context, OrderDetailScreen(id: orderList[index].orderId)).then((value) {
                  if(value!= null){
                    orderList.removeAt(index);
                    if(orderList.isEmpty){
                      getOrderList();
                    }
                    setState(() {

                    });
                  }
                });
              },child: itemWidget(index),);
            }),
      ),
    );
  }


  Widget itemWidget(int index) {
    ListElement element = orderList[index];
    String caption = element.caption;
    String type = element.status;
    String price = element.totalPrice;
    String time = '${element.orderAt.year}-${element.orderAt.month}-${element.orderAt.day} ';
    return Padding(
      padding: EdgeInsets.only(left: 24.px, top: 24.px, right: 24.px,),
      child: ClipRRect(borderRadius: BorderRadius.all(Radius.circular(8.px)), child: Container(
        color: Colors.white,
        padding: EdgeInsets.all(20.px),
        width: double.infinity,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(children: [
              BaseImage(image: 'order_line',width: 35.px,height: 35.px,),
              SizedBox(width: 20.px,),
              Expanded(child: Text(
                '路书',
                style: TextStyle(
                    fontSize: 30.px,
                    color: MyColors.color333333,
                    fontWeight: FontWeight.w500),
              ),),
              Text(
                type,
                style: TextStyle(
                    fontSize: 30.px,
                    color: MyColors.color333333,
                    fontWeight: FontWeight.w500),
              ),
            ],),
            SizedBox(height: 20.px,),
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Expanded(
                  child:Text(
                    caption,
                    style: TextStyle(
                        fontSize: 30.px,
                        color: MyColors.color333333,
                        fontWeight: FontWeight.w500),
                  ),
                ),
                SizedBox(
                  width: 30.px,
                ),
                Text(
                  '￥$price',
                  style: TextStyle(
                    fontSize: 30.px,
                    color: MyColors.colorred,
                  ),
                ),
              ],
            ),
            SizedBox(
              height: 20.px,
            ),
            Row(children: [
              if (time != null)
                Text(
                  time ,
                  style: TextStyle(
                    fontSize: 28.px,
                    color: MyColors.color666666,
                  ),
                ),
              Expanded(child:  Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  GestureDetector(
                    onTap: () {
                      PagePushUtils.push(context, LineDetailPageScreen(id: orderList[index].orderData!.id));
                    },
                    child: Container(
                      //设置 child 居中
                      alignment: Alignment.center,
                      height: 50.px,
                      width: 130.px,
                      //边框设置
                      decoration: BoxDecoration(
                        //背景
                        color: Colors.white,
                        //设置四周圆角 角度
                        borderRadius:
                        BorderRadius.all(Radius.circular(4.px)),
                        //设置四周边框
                        border: Border.all(
                            width: 1, color: MyColors.appColor),
                      ),
                      child: Text(
                        "查看路书",
                        style: TextStyle(
                            fontSize: 26.px,
                            color: MyColors.appColor),
                      ),
                    ),
                  ),
                  SizedBox(
                    width: 16.px,
                  ),
                  GestureDetector(
                    onTap: () {
                      delOrder(index);
                    },
                    child: Container(
                      //设置 child 居中
                      alignment: Alignment.center,
                      height: 50.px,
                      width: 100.px,
                      //边框设置
                      decoration: BoxDecoration(
                        //背景
                        color: Colors.white,
                        //设置四周圆角 角度
                        borderRadius:
                        BorderRadius.all(Radius.circular(4.px)),
                        //设置四周边框
                        border: Border.all(
                            width: 1, color: MyColors.colorred),
                      ),
                      child: Text(
                        "删除",
                        style: TextStyle(
                            fontSize: 26.px,
                            color: MyColors.colorred),
                      ),
                    ),
                  ),
                ],
              ))

            ],)
            // ignore: unnecessary_null_comparison

          ],
        ),
      ),),);
  }


  void getOrderList() {
    Map<String, dynamic> params = Map();
    params['type'] = 2;
    AhaHttp().get(AhaRouter.MyOrderList, params: params).then((value) {
      print(value);
      OrderListModel model = OrderListModel.fromJson(value);
      _controller.refreshEnd(page: page, maxPage: model.meta.maxPage);
      if (page == 1) {
        orderList.clear();
      }
      orderList.addAll(model.list);
      if (orderList.isEmpty) {
        loadingType = 1;
      }
      setState(() {});
    });
  }
  void delOrder(int itemIndex) {
    showMyDialog(context, '是否删除该订单', "是", "否", (index) {
      if (index == 0) {
        Map<String, dynamic> params = Map();
        params['orderId'] = orderList[itemIndex].orderId;
        AhaHttp().post(AhaRouter.MyOrderDelete, data: params).then((value) {
          if(value != null){
            orderList.removeAt(itemIndex);
            setState(() {});
          }
        });
      }
    });

  }

}
